﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;

namespace CardsServer.WCF
{
    public partial interface IService
    {
        [OperationContract]
        string Goods_Add(Goods model, int ACID);


        [OperationContract]
        string Goods_Update(Goods model, int ACID);


        [OperationContract]
        string GoodsByGoodsCode_Delete(string GoodsCode, int ACID);


        [OperationContract]
        Goods GoodsByGoodsCode_Get(string GoodsCode, int ACID);


        [OperationContract]
        List<Goods> Goods_Get(string sortBy, int ACID);

        [OperationContract]
        List<Goods> Goods_Get_byClassid(string classid, string sortBy, int ACID);


    }
    public partial class Service : IService
    {
        #region  添加Goods表数据
        /// <summary>
        /// 添加Goods
        /// </summary>
        /// <param name="model">Goods实体</param>
        /// <returns>空或错误</returns>
        public string Goods_Add(Goods model, int ACID)
        {
            if (string.IsNullOrEmpty(model.GoodsCode))
            {
                return "『产品编号』不能为空！";
            }
            if (model.Kind == null)
            {
                return "『存货类型』不能为空！";
            }
            if (model.AveragePrice == null)
            {
                return "『加权价』不能为空！";
            }
            if (!model.isBlankCard.HasValue)
            {
                return "『空电话卡标识』不能为空！";
            }
            if (string.IsNullOrEmpty(model.Unit))
            {
                return "『单位』不能为空！";
            }
            if (string.IsNullOrEmpty(model.GoodsName))
            {
                return "『产品名称』不能为空！";
            }
            if (string.IsNullOrEmpty(model.ClassID))
            {
                return "『产品类别编号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.Goods.Count(p => p.GoodsCode == model.GoodsCode) > 0)
                    {
                        return "主键已存在！";
                    }
                    ctx.Goods.InsertOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 添加失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  添加Goods表数据事物用重构
        /// <summary>
        /// 添加Goods事物用重构
        /// </summary>
        /// <param name="model">Goods实体</param>
        /// <returns>空或错误</returns>
        private string Goods_Add(Goods model, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(model.GoodsCode))
            {
                return "『产品编号』不能为空！";
            }
            if (model.Kind == null)
            {
                return "『存货类型』不能为空！";
            }
            if (model.AveragePrice == null)
            {
                return "『加权价』不能为空！";
            }
            if (!model.isBlankCard.HasValue)
            {
                return "『空电话卡标识』不能为空！";
            }
            if (string.IsNullOrEmpty(model.Unit))
            {
                return "『单位』不能为空！";
            }
            if (string.IsNullOrEmpty(model.GoodsName))
            {
                return "『产品名称』不能为空！";
            }
            if (string.IsNullOrEmpty(model.ClassID))
            {
                return "『产品类别编号』不能为空！";
            }
            if (ctx.Goods.Count(p => p.GoodsCode == model.GoodsCode) > 0)
            {
                return "主键已存在！";
            }
            ctx.Goods.InsertOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  修改Goods表数据
        /// <summary>
        /// 修改Goods
        /// </summary>
        /// <param name="model">Goods实体</param>
        /// <returns>空或错误</returns>
        public string Goods_Update(Goods model, int ACID)
        {
            if (string.IsNullOrEmpty(model.GoodsCode))
            {
                return "『产品编号』不能为空！";
            }
            if (model.Kind == null)
            {
                return "『存货类型』不能为空！";
            }
            if (model.AveragePrice == null)
            {
                return "『加权价』不能为空！";
            }
            if (!model.isBlankCard.HasValue)
            {
                return "『空电话卡标识』不能为空！";
            }
            if (string.IsNullOrEmpty(model.Unit))
            {
                return "『单位』不能为空！";
            }
            if (string.IsNullOrEmpty(model.GoodsName))
            {
                return "『产品名称』不能为空！";
            }
            if (string.IsNullOrEmpty(model.ClassID))
            {
                return "『产品类别编号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.Goods.Count(p => p.GoodsCode == model.GoodsCode) != 1)
                    {
                        return "未找到指定记录或记录不唯一！";
                    }
                    Goods Goodsmodel = ctx.Goods.Single(p => p.GoodsCode == model.GoodsCode);
                    if (!string.IsNullOrEmpty(model.GoodsCode))
                    {
                        Goodsmodel.GoodsCode = model.GoodsCode;
                    }
                    if (!string.IsNullOrEmpty(model.ClassID))
                    {
                        Goodsmodel.ClassID = model.ClassID;
                    }
                    if (model.ClientID != null || model.ClientID.Equals(Guid.Empty))
                    {
                        Goodsmodel.ClientID = model.ClientID;
                    }

                        Goodsmodel.isBlankCard = model.isBlankCard;

                    if (!string.IsNullOrEmpty(model.GoodsName))
                    {
                        Goodsmodel.GoodsName = model.GoodsName;
                    }
                    if (!string.IsNullOrEmpty(model.EGoodsName))
                    {
                        Goodsmodel.EGoodsName = model.EGoodsName;
                    }
                    if (!string.IsNullOrEmpty(model.Unit))
                    {
                        Goodsmodel.Unit = model.Unit;
                    }
                    if (model.MaxStock != null)
                    {
                        Goodsmodel.MaxStock = model.MaxStock;
                    }
                    if (model.MinStock != null)
                    {
                        Goodsmodel.MinStock = model.MinStock;
                    }
                    if (model.AveragePrice != null)
                    {
                        Goodsmodel.AveragePrice = model.AveragePrice;
                    }
                    if (model.Kind != null)
                    {
                        Goodsmodel.Kind = model.Kind;
                    }
                    if (!string.IsNullOrEmpty(model.Spec))
                    {
                        Goodsmodel.Spec = model.Spec;
                    }
                    if (!string.IsNullOrEmpty(model.Location))
                    {
                        Goodsmodel.Location = model.Location;
                    }
                    if (!string.IsNullOrEmpty(model.ZJM))
                    {
                        Goodsmodel.ZJM = model.ZJM;
                    }
                    if (!string.IsNullOrEmpty(model.Barcode))
                    {
                        Goodsmodel.Barcode = model.Barcode;
                    }
                    if (model.inprice != null)
                    {
                        Goodsmodel.inprice = model.inprice;
                    }
                    if (model.outprice != null)
                    {
                        Goodsmodel.outprice = model.outprice;
                    }
                    if (model.LowPrice != null)
                    {
                        Goodsmodel.LowPrice = model.LowPrice;
                    }
                    if (model.HighPrice != null)
                    {
                        Goodsmodel.HighPrice = model.HighPrice;
                    }
                    if (model.DealerPrice1 != null)
                    {
                        Goodsmodel.DealerPrice1 = model.DealerPrice1;
                    }
                    if (model.DealerPrice2 != null)
                    {
                        Goodsmodel.DealerPrice2 = model.DealerPrice2;
                    }
                    if (model.DealerPrice3 != null)
                    {
                        Goodsmodel.DealerPrice3 = model.DealerPrice3;
                    }
                    if (model.DealerPrice4 != null)
                    {
                        Goodsmodel.DealerPrice4 = model.DealerPrice4;
                    }
                    if (model.DealerPrice5 != null)
                    {
                        Goodsmodel.DealerPrice5 = model.DealerPrice5;
                    }
                    if (model.VipPrice1 != null)
                    {
                        Goodsmodel.VipPrice1 = model.VipPrice1;
                    }
                    if (model.VipPrice2 != null)
                    {
                        Goodsmodel.VipPrice2 = model.VipPrice2;
                    }
                    if (model.VipPrice3 != null)
                    {
                        Goodsmodel.VipPrice3 = model.VipPrice3;
                    }
                    if (!string.IsNullOrEmpty(model.Custom1))
                    {
                        Goodsmodel.Custom1 = model.Custom1;
                    }
                    if (!string.IsNullOrEmpty(model.Custom2))
                    {
                        Goodsmodel.Custom2 = model.Custom2;
                    }
                    if (!string.IsNullOrEmpty(model.Custom3))
                    {
                        Goodsmodel.Custom3 = model.Custom3;
                    }
                    if (!string.IsNullOrEmpty(model.Custom4))
                    {
                        Goodsmodel.Custom4 = model.Custom4;
                    }
                    if (!string.IsNullOrEmpty(model.Custom5))
                    {
                        Goodsmodel.Custom5 = model.Custom5;
                    }
                    if (!string.IsNullOrEmpty(model.Remarks))
                    {
                        Goodsmodel.Remarks = model.Remarks;
                    }
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 修改失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion



        #region  删除Goods表ByGoodsCode数据
        /// <summary>
        /// 删除GoodsByGoodsCode
        /// </summary>
        /// <param name="GoodsCode">产品编号</param>
        /// <returns>空或错误</returns>
        public string GoodsByGoodsCode_Delete(string GoodsCode, int ACID)
        {
            if (string.IsNullOrEmpty(GoodsCode))
            {
                return "『产品编号』不能为空！";
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.Goods.Count(p => p.GoodsCode == GoodsCode) != 1)
                    {
                        return null;
                    }
                    Goods model = ctx.Goods.Single(p => p.GoodsCode == GoodsCode);
                    ctx.Goods.DeleteOnSubmit(model);
                    ctx.SubmitChanges();
                    return string.Empty;
                }
                catch (Exception ex)
                {
                    return " 删除失败!错误详细信息：" + ex.Message;
                }
            }
        }
        #endregion

        #region  删除Goods表ByGoodsCode数据[事物用重构]
        /// <summary>
        /// 删除GoodsByGoodsCode[事物用重构]
        /// </summary>
        /// <param name="GoodsCode">产品编号</param>
        /// <returns>空或错误</returns>
        private string GoodsByGoodsCode_Delete(string GoodsCode, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            if (string.IsNullOrEmpty(GoodsCode))
            {
                return "『产品编号』不能为空！";
            }
            if (ctx.Goods.Count(p => p.GoodsCode == GoodsCode) != 1)
            {
                return null;
            }
            Goods model = ctx.Goods.Single(p => p.GoodsCode == GoodsCode);
            ctx.Goods.DeleteOnSubmit(model);
            return string.Empty;
        }
        #endregion



        #region  获取Goods表ByGoodsCode数据
        /// <summary>
        /// 获取GoodsByGoodsCode
        /// </summary>
        /// <param name="GoodsCode">产品编号</param>
        /// <returns>当前表</returns>
        public Goods GoodsByGoodsCode_Get(string GoodsCode, int ACID)
        {
            if (string.IsNullOrEmpty(GoodsCode))
            {
                return null;
            }
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (ctx.Goods.Count(p => p.GoodsCode == GoodsCode) != 1)
                    {
                        return null;
                    }
                    return ctx.Goods.SingleOrDefault(p => p.GoodsCode == GoodsCode);
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion



        #region  获取Goods表数据
        /// <summary>
        /// 获取Goods
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<Goods></returns>
        public List<Goods> Goods_Get(Expression<Func<Goods, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.Goods.ToList();
                        }
                        else
                        {
                            return ctx.Goods.OrderBy(sortBy).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.Goods.Where(predicate).ToList();
                        }
                        else
                        {
                            return ctx.Goods.Where(predicate).OrderBy(sortBy).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取Goods表数据
        /// <summary>
        /// 获取Goods
        /// </summary>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<Goods></returns>
        public List<Goods> Goods_Get(string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    return Goods_Get(null, sortBy, ACID);
                }
                catch (Exception)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  获取Goods表数据计数
        /// <summary>
        /// 获取Goods表数据计数
        /// </summary>
        /// <returns>List<Goods></returns>
        public int Goods_GetCount(Expression<Func<Goods, bool>> predicate, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        return ctx.Goods.Count();

                    }
                    else
                    {
                        return ctx.Goods.Where(predicate).Count();
                    }
                }
                catch
                {
                    return 0;
                }
            }
        }
        #endregion




        #region  获取Goods分页表数据
        /// <summary>
        /// 获取Goods分页表数据
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="sortBy">排序属性名</param>
        /// <param name="sortType">排序类型0=升序 1=降序</param>
        /// <returns>List<Goods></returns>
        public List<Goods> Goods_Getpage(int pageIndex, int pageSize, Expression<Func<Goods, bool>> predicate, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                try
                {
                    if (predicate == null)
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.Goods.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.Goods.OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                    else
                    {
                        if (string.IsNullOrWhiteSpace(sortBy))
                        {
                            return ctx.Goods.Where(predicate).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                        else
                        {
                            return ctx.Goods.Where(predicate).OrderBy(sortBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    return null;
                }
            }
        }
        #endregion


        #region  批量修改Goods表数据
        /// <summary>
        /// 批量修改Goods
        /// </summary>
        /// <param name="predicate">Expression<Func<Goods, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<Goods,Goods>> 更新表达式树</param>
        /// <param name="returns">影响记录数</param>
        public int Goods_batchUpdate(Expression<Func<Goods, bool>> predicate, Expression<Func<Goods, Goods>> updater, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.Goods.Update(predicate, updater);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量修改Goods表数据事物用重构
        /// <summary>
        /// 批量修改Goods事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<Goods, bool>></param> 查找表达式树
        /// <param name="updater">Expression<Func<Goods,Goods>> 更新表达式树</param>
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int Goods_batchUpdate(Expression<Func<Goods, bool>> predicate, Expression<Func<Goods, Goods>> updater, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.Goods.Update(predicate, updater);
            return ok;
        }
        #endregion


        #region  批量删除Goods表数据
        /// <summary>
        /// 批量删除Goods
        /// </summary>
        /// <param name="predicate">Expression<Func<Goods, bool>></param> 查找表达式树
        /// <param name="returns">影响记录数</param>
        public int Goods_batchDel(Expression<Func<Goods, bool>> predicate, int ACID)
        {
            using (TransactionScope ts = new TransactionScope())
            {
                using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                {
                    try
                    {
                        int ok = ctx.Goods.Delete(predicate);
                        ts.Complete();
                        return ok;
                    }
                    catch (Exception ex)
                    {
                        return 0;
                    }
                }
            }
        }
        #endregion


        #region  批量删除Goods表数据事物用重构
        /// <summary>
        /// 批量删除Goods事物用重构
        /// </summary>
        /// <param name="predicate">Expression<Func<Goods, bool>></param> 查找表达式树
        /// <param name="ctx">数据上下文</param>
        /// <param name="returns">影响记录数</param>
        private int Goods_batchDel(Expression<Func<Goods, bool>> predicate, int ACID, CardsServer.DAL.DataClassesDataContext ctx)
        {
            int ok = ctx.Goods.Delete(predicate);
            return ok;
        }
        #endregion

        public List<Goods> Goods_Get_byClassid(string classid, string sortBy, int ACID)
        {
            using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
            {
                return Goods_Get(p => p.ClassID.StartsWith(classid), sortBy, ACID);
            }

        }
    }
}

